import { observable, action } from 'mobx';
import { toJS } from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import { Form, CKRichText, ListAdvanced, ListViewPage, SearchAdvanced, RouteLayout, Reply } from 'weaver-mobile-page';

const getLabel = Tools.getLabel;

export default class SmsRadioStore {
  @observable selectKey = 0;
  @observable form = new Form.Store({ route: '/sms-radio' });
  @observable search = new SearchAdvanced.Store({ route: '/sms-radio' });
  @observable receiverSearch = new SearchAdvanced.Store({ route: '/sms-receive' });

  @observable formConditions = [];
  @observable titleConditions =
    {
      colSpan: 2,
      conditionType: "INPUT",
      domkey: ["title"],
      fieldcol: 17,
      formItemType: "INPUT",
      label: getLabel('514515', '广播标题'),
      labelcol: 6,
      rules: "required",
      value: "",
      viewAttr: 3,
      // length:1,
      // maxLength:2,
      // stringLength:1
    };
  @observable loading = false;
  @observable animating = false;

  // [
  //   {
  //     browserConditionParam: {},
  //     browserType: "242",
  //     colSpan: 2,
  //     conditionType: "BROWSER",
  //     domkey: ["type"],
  //     fieldcol: 17,
  //     formItemType: "BROWSER",
  //     label: "广播类型",
  //     labelcol: 6,
  //     rules: "required",
  //     value: "",
  //     viewAttr: "3"
  //   },
  //   {
  //     colSpan: 2,
  //     conditionType: "SELECT",
  //     domkey: ["receive"],
  //     fieldcol: 17,
  //     formItemType: "SELECT",
  //     label: "广播接收人",
  //     labelcol: 6,
  //     options: [
  //       {
  //         key: "0",
  //         showname: "全部",
  //       },
  //       {
  //         key: "1",
  //         showname: "指定人",
  //       },
  //     ],
  //     rules: "required",
  //     value: "0",
  //     viewAttr: 2
  //   },
  //   {
  //     browserConditionParam: {},
  //     browserType: "242",
  //     colSpan: 2,
  //     conditionType: "BROWSER",
  //     domkey: ["receiveMan"],
  //     fieldcol: 17,
  //     formItemType: "BROWSER",
  //     label: "指定人",
  //     labelcol: 6,
  //     rules: "required",
  //     value: "",
  //     viewAttr: "3"
  //   },
  // {
  //   colSpan: 2,
  //   conditionType: "INPUT",
  //   domkey: ["title"],
  //   fieldcol: 17,
  //   formItemType: "INPUT",
  //   label: "广播标题",
  //   labelcol: 6,
  //   length: 500,
  //   rules: "required",
  //   value: "",
  //   viewAttr: 3
  // };
  //   ,
  // ]
  @observable reply = new Reply.Store({ getAttachList: this.getReplyAttachList });
  @observable ckRichTextStore = new CKRichText.Store({
    reply: this.reply,
  });


  @observable listV = new ListViewPage.Store({ route: '/sms-radio' });

  @observable receiverListV = new ListViewPage.Store({ route: '/sms-receive' });
  @observable receiverSearchLayout = new RouteLayout.Store({ route: '/sms-receive' });

  @observable detailItem = {};
  @observable workFlowRes = {};
  @observable docRes = {};
  @observable fileRes = {};

  @observable ckVal = '';

  @observable sendRadioParams = {
    msgType: '',
    title: '',
    contexts: '',
    receiverid: '',
  }

  @observable layout = new RouteLayout.Store({ route: '/sms-radio' });

  @observable searchLayout = new RouteLayout.Store({ route: '/sms-radio', id:'search'});

  @observable workFlowBiz = '';
  @observable docBiz = '';
  @observable fileBiz = '';
  @observable changedatas = {};
  @observable submitdatas = {};
  @observable workFlowReplaceData = [];
  @observable docReplaceData = [];
  @observable fileData = [];

  getReplyAttachList = () => {
    const browserConfig = {
      type: 'browser',
      path: '/sms-radio',
      model: 1, // model(模式) 1: 常规模式， 2: 编辑模式（不显示小红点）
    };
    const attachList = [
      {
        ...browserConfig,
        key: 'workflow', // 字符串
        config: {
          type: 152,
          isSingle: false,
          hasAdvanceSerach: true,
          valueArray: this.workFlowReplaceData
        },
        iconType: 'flow',
        iconBgColor: '#61A8F5',
        text: getLabel('30045', '流程'),
      },
      {
        ...browserConfig,
        key: 'doc',
        config: {
          type: 37,
          isSingle: false,
          valueArray: this.docReplaceData
        },
        iconType: 'file',
        iconBgColor: '#F3B04C',
        text: getLabel('22243', '文档'),
      },
      {
        key: 'upload',
        type: 'upload',
        path: '/sms-radio',
        config: {
          multiSelection: true,
          datas: this.fileData,
          uploadUrl: '/api/doc/upload/uploadFile2Doc',
          maxUploadSize: 100,
        },
        iconType: 'enclosure',
        iconBgColor: '#57D58F',
        text: getLabel('156', '附件'),
        model: 1,
      },
    ];
    return new Promise(resolve => resolve({ attachList }));
  }

  //根据广播broadcastid查询单条广播详情
  @action
  getSmsDetailByBroadcastid = (id) => {
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
    })
    return Tools.callApi({
      url: '/api/msgcenter/broadcast/getBroadcastRecordByBcastId',
      method: 'POST',
      params: { broadcastid: id },
    }).then(res => {
      this.detailItem = res;
      if (res.workFlowBiz) {
        this.workFlowBiz = res.workFlowBiz.workFlowBizList;
      }
      if (res.docBiz) {
        this.docBiz = res.docBiz.docBizList;
      }
      if (res.fileBiz) {
        this.fileBiz = res.fileBiz.fileBizList;
      }
    });
  }

  //根据广播id查询单条广播详情
  @action
  getSmsDetailById = (id) => {
    this.setState({
      workFlowBiz: '',
      docBiz: '',
      fileBiz: '',
      workFlowReplaceData: [],
      docReplaceData: [],
      fileData: [],
      workFlowRes:{},
      docRes:{},
      fileRes:{},
    })
    return Tools.callApi({
      url: '/api/msgcenter/broadcast/getBroadcastRecord',
      method: 'POST',
      params: { id: id },
    }).then(res => {
      this.detailItem = res;
      if (res.workFlowBiz) {
        this.workFlowBiz = res.workFlowBiz.workFlowBizList;
        this.setState({ workFlowReplaceData: res.workFlowJson });
        this.workFlowRes = res;
      }
      if (res.docBiz) {
        this.docBiz = res.docBiz.docBizList;
        this.setState({ docReplaceData: res.docJson });
        this.docRes = res
      }
      if (res.fileBiz) {
        this.fileBiz = res.fileBiz.fileBizList;
        this.setState({ fileData: res.fileJson });
        this.fileRes = res
      }
    });
  }

  //获取发送广播页面的两个item（广播类型，广播接收人）
  @action
  getSendBroadcastCondition = (msgtype) => {
    return Tools.callApi({
      url: '/api/msgcenter/broadcast/mobile/getSendBroadcastCondition',
      method: 'GET',
      params: { msgtype: msgtype },
    }).then(res => {
      if (res.status) {
        res.condition[0].items.push(this.titleConditions);
        this.formConditions = res.condition;
        this.form.initForm(this.formConditions, '/sms-radio');
      }
    });

  }
  //发送广播，提交广播信息并发送消息
  @action
  submitBroadcast = (information) => {
    if (information.receivertype == 0) {//广播接收人选了全部
      this.sendRadioParams.receiverid = -1;
    }
    if (information.receivertype == 1) {///广播接收人选了指定人
      if (information.receiver) {
        this.sendRadioParams.receiverid = information.receiver
      }
    }
    this.sendRadioParams.contexts = this.ckRichTextStore.text;
    this.sendRadioParams.msgType = information.type;
    this.sendRadioParams.title = information.title;
    this.sendRadioParams.biz = JSON.stringify(this.changedatas);
    // console.log(this.sendRadioParams)
    // console.log(this.changedatas);

    this.animating = true;
    return Tools.callApi({
      url: '/api/msgcenter/broadcast/sendBroadcastRecord',
      method: 'POST',
      params: this.sendRadioParams,
    }).then(res => {
      if (res.status) {
        this.setState({
          changedatas: {},
          fileData: [],
          docReplaceData: [],
          workFlowReplaceData: [],
        })
        this.animating = false;
        this.form.resetFields();
        this.ckRichTextStore.clear();
      }
    });
  }

  // 广播记录高级搜索表单
  @action getRecordAdvanceCondition = () => {
    this.search.getData({
      url: '/api/msgcenter/broadcast/mobile/getBroadcastRecordCondition',
      method: 'GET',
    })
  }

  // 接收人高级搜索表单
  @action getReceiverAdvanceCondition = () => {
    this.receiverSearch.getData({
      url: '/api/msgcenter/broadcast/mobile/getBroadcastReceiverCondition',
      method: 'GET',
    })

  }

  //获取广播记录数据table
  @action
  getData = (params = { url: '', params: {} }) => {
    try {
      Tools.callApi({
        ...params,
        params: {
          ...params.params,
          ismobile: 1,
        },
      }).then(action((res) => {
        const { datas: dataKey } = res;
        // table session key
        if (dataKey) {
          this.dataKey = dataKey;
        }
        this.listV.getData({ dataKey });
      }));
    } catch (e) {
      Toast.info(e, 1);
    }
  }
  //获取接收人数据table
  @action
  receiverGetData = (params = { url: '', params: {} }) => {
    try {
      Tools.callApi({
        ...params,
        params: {
          ...params.params,
          ismobile: 1,
        },
      }).then(action((res) => {
        const { datas: dataKey } = res;
        // table session key
        if (dataKey) {
          this.dataKey = dataKey;
        }
        this.receiverListV.getData({ dataKey });
      }));
    } catch (e) {
      Toast.info(e, 1);
    }
  }


  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }
}